<template>
    <div class="auLayout">
        <form class="form">
            <div class="title">添加管理员</div>
            <div class="input">
                <input placeholder="用户名" v-model="formInfo.uname"/>
            </div>
            <div class="input">
                <input placeholder="密码" v-model="formInfo.password" type="password"/>
            </div>
            <div class="input">
                <input placeholder="联系电话" v-model="formInfo.phone"/>
            </div>

            <button class="button" @click="addUser" :disabled="isDisabled">立即注册</button>
        </form>
    </div> 
</template>

<script lang="ts" setup>
import { reactive, computed } from 'vue'
import { get,post } from '@/http/axios'
import { ElMessage } from 'element-plus'

const formInfo = reactive({
    uname:null,
    password:null,
    phone:null
})
function addUser() {
    post('/admin/addAdmin',{
        code: formInfo.phone,
        name: formInfo.uname,
        password: formInfo.password
    }).then(res => {
        if (res.result) {
            ElMessage.success('管理员添加成功')
            return clear()
        }
    }).catch(err => {
        ElMessage.error('管理员添加失败， ' + err)
    })
}
const isDisabled = computed(() => {
    let flag = false
    Object.keys(formInfo).forEach(el => {
        if (!formInfo[el]) {
            flag = true
        } 
    })
    return flag
})
function clear() {
    Object.keys(formInfo).forEach(el => {
        formInfo[el] = null
    })
}
</script>

<style lang="scss" scoped>
.auLayout{
    width: 100%;
    @include flexColumnCenter;
    gap: 2px;
    @include allBack;
}
.form{
    padding: 40px;
    background-color: white;
    position: relative;
    min-width: 350px;
    height: 100%;
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 25px;
}
.title{
    color: #409EFF;
    font-size: 30px;
    font-weight: 600;
}
.input{
    position: relative;
    width: 80%;
    height: 35px;
    @include flexColumnCenter;
    opacity: 0.8;
    & input:nth-child(1){
        outline: none;
        height: 35px;
        width: 80%;
        border: 3px solid var(--el-border-color);
        border-radius: 8px;
    }
    & span:nth-child(2){
        position: absolute;
        display: none;
        bottom: -20px;
        font-size: 14px;
        line-height: 18px;
        color: red;
        width: 80%;
        text-align: left;
    }
}
.button{
    outline: none;
    width: 120px;
    height: 40px;
    border-radius: 4px;
    background: #409EFF;
    color: white;
    &:disabled{
        background: #93b2c7;
    }
    &:hover{
        border: 1px solid #93b2c7;
    }
}
.addA{
    position: absolute;
    bottom: 10px;
    right: 20px;
    color: rgb(8, 7, 7);
    &:hover{
        color: #409EFF;
    }
}
</style>